package cn.zysuyuan.auth.service.impl;

import cn.zysuyuan.auth.client.RoleClient;
import cn.zysuyuan.auth.client.UserClient;
import cn.zysuyuan.auth.config.JwtProperties;
import cn.zysuyuan.auth.entity.UserInfo;
import cn.zysuyuan.auth.service.AuthService;
import cn.zysuyuan.auth.utils.JwtUtils;
import cn.zysuyuan.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @author flypig
 * @version 1.0
 * @date 2019-11-26 2:05
 */

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RoleClient roleClient;

    @Override
    public String accredit(String uName, String uPass) {
        // 1. 根据用户名和密码查询
        User user = this.userClient.selectUser(uName,uPass);
        Map<String,Object> map = roleClient.findRoles(user.getLevel(),user.getId());
        System.out.println(user);

        // 2. 判断user
        if (user == null){
            return null;
        }
        try {
            // 3. jwtUtils生产jwt类型的token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId());
            userInfo.setUsername(user.getuName());
//            userInfo.setRoles(user.getzRoles());
//            userInfo.setRoles(map);
            userInfo.setNickname(user.getuNickname());
            userInfo.setHeadpath(user.getuHeadpath());
            userInfo.setRemark(user.getzRemark());
            userInfo.setEmail(user.getuEmail());
            userInfo.setPhone(user.getuContact());
            userInfo.setLevel(user.getLevel());
//            System.out.println(userInfo);
            return JwtUtils.generateToken(userInfo,this.jwtProperties.getPrivateKey(),this.jwtProperties.getExpire());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
